<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入合同编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入合同名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="checkStatus">
          <el-select v-model="queryParams.checkStatus" placeholder="请选择审核状态" clearable size="small">
           <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed label="合同编号" align="center" prop="code" />
     <el-table-column label="合同名称" align="center" prop="name" :show-overflow-tooltip="true">
     </el-table-column>
     <el-table-column label="合同金额" align="center" prop="money" width="120px">
        <template slot-scope="scope">
          <span class="money">{{formatMoney(scope.row.money,',')}}</span>
        </template>
    </el-table-column>
     <el-table-column label="客户名称" align="center" prop="customerName" :show-overflow-tooltip="true" >
       <template slot-scope="scope">
           <el-link @click="openCustomer(scope.row)" :underline="false" type="primary" >{{scope.row.customerName}}</el-link>
        </template>
     </el-table-column>
    <el-table-column label="下单时间" align="center" prop="orderDate" width="180">
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
      </template>
    </el-table-column>
    <el-table-column label="合同开始时间" align="center" prop="startTime" width="180">
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
      </template>
    </el-table-column>
    <el-table-column label="合同结束时间" align="center" prop="limitTime" width="180">
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
      </template>
    </el-table-column>
     <!-- <el-table-column label="客户签约人" align="center" prop="customerName" /> -->
     <el-table-column label="备注" align="center" prop="remark" />
     <el-table-column label="负责人" align="center" prop="nickName" />
      <el-table-column label="状态" align="center" prop="checkStatus" >
        <template slot-scope="scope">
              <el-tag prop="checkStatus" v-for="(item, index) in statusOptions"  :type="scope.row.checkStatus==1?'success':'danger'"  v-if="scope.row.checkStatus==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditNickName" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="auditNote" />
      <el-table-column  label="操作" align="center" class-name="small-padding fixed-width"  fixed="right" width="80">
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.checkStatus==0&&scope.row.status==0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['crm:contract:audit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-log"
            @click="handleShow(scope.row)"
            v-hasPermi="['crm:contract:query']"
          >查看合同</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
   
     <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="rules" label-width="80px">
              <el-form-item label="合同名称" prop="name">
                <span>{{auditForm.name}}</span>
              </el-form-item>
              <el-form-item label="客户名称" prop="customerName">
                <span>{{auditForm.customerName}}</span>
              </el-form-item>
              <el-form-item label="负责人" prop="nickName">
                <span>{{auditForm.nickName}}</span>
              </el-form-item>
              <el-form-item label="下单时间" prop="customerName">
                <span>{{parseTime(auditForm.orderDate, '{y}-{m}-{d} {h}:{i}:{s}')}}</span>
              </el-form-item>
              <el-form-item label="状态">
                <el-radio-group v-model="auditForm.checkStatus">
                  <el-radio  key="1" label="1">通过</el-radio>
                  <el-radio  key="2" label="2">拒绝</el-radio>
                </el-radio-group>
              </el-form-item>
          </el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="auditForm.auditNote" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-drawer
     size="50%"
     :with-header="false"
      :title="customerShow.title" :visible.sync="customerShow.open"
      >
        <customer  ref="customer" />
    </el-drawer>
    <el-drawer
     size="50%"
     :with-header="false"
      :title="contractShow.title" :visible.sync="contractShow.open"
      >
        <contract  ref="contract" />
    </el-drawer>
   
  </div>
</template>

<script>
import { auditListContract,auditContract } from "@/api/crm/contract";

import { getToken } from "@/utils/auth";
import customer from "../../components/customerDetails";
import contract from "../../components/contractDetails";
 

export default {
  name: "Customer",
  components: { customer,contract},
  data() {
    return {
      customerShow:{
        open:false,
        title:'合同详情'
      },
      contractShow:{
        open:false,
        title:'合同详情'
      },
      contractTypeOptions:[],
      statusOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code:null,
        name:null,
        customerName: null,
        mobile: null,
      },
      auditForm:{

      },
      rules:{
        
      }
      
    };
  },
  created() {
    this.getList();
    this.getDicts("contract_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("contract_type").then((response) => {
      this.contractTypeOptions = response.data;
    });
     
  },
  mounted() {
   
  },
  methods: {
    
    handleAudit(row){
      this.open = true;
      this.title = "审核合同";
      this.auditForm={
        name:null,
        customerName:null,
        nickName:null,
        contractId:null,
        orderDate:null,
        checkStatus:'1',
        auditNote:null,
      };
      this.auditForm.name=row.name;
      this.auditForm.customerName=row.customerName;
      this.auditForm.nickName=row.nickName;
      this.auditForm.contractId=row.contractId;
      this.auditForm.orderDate=row.orderDate;
    },
    openCustomer(row){
      
      this.customerShow.open=true;
      const customerId = row.customerId || this.ids
      setTimeout(() => {
        this.$refs.customer.getCustomerDetails(customerId);
      }, 500);
    },
    handleShow(row) {
      this.contractShow.open=true;
      const contractId = row.contractId || this.ids
      setTimeout(() => {
        this.$refs.contract.getContract(contractId);
      }, 500);
      
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      auditListContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     
     
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contractId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    submitForm() {
       auditContract(this.auditForm).then(response => {
              if (response.code === 200) {
                this.msgSuccess("操作成功");
                this.open = false;
                this.getList();
              }
            });
    },
    cancel() {
      this.open = false;
     
    },
    
  }
};
</script>
<style scoped>
 

  .money{
      color: red;
  }
</style>

